//
//  UserInfo.m
//  ios-supplier
//
//  Created by 梁泽 on 16/3/10.
//  Copyright © 2016年 feynmanyuan. All rights reserved.
//

#import "UserInfo.h"

#define kUT            @"kUT"
#define kmerchantID    @"kmerchantID"
#define kcontactAddr   @"kcontactAddr"
#define kcontactMobile @"kcontactMobile"
#define kcontactName   @"kcontactName"
#define kshopkeeper    @"kshopkeeper"
#define kshopStatus    @"kshopStatus"

#define LZString(str) [NSString stringWithFormat:@"%@",str]
@implementation UserInfo
+ (instancetype)currentUser
{
    static UserInfo *currentUser = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        currentUser = [[UserInfo alloc] init];
    });
    return currentUser;
}


- (id)init
{
    self = [super init];
    if (self)
    {
        [self loadUserInfoFromDisk];
    }
    return self;
}

- (void)setupWithDictionary:(NSDictionary *)info;
{
    if (info)
    {
        self.merchantID    = LZString(info[@"merchantId"]);
        self.contactAddr   = LZString(info[@"contactAddr"]);
        self.contactMobile = LZString(info[@"contactMobile"]);
        self.contactName   = LZString(info[@"name"]);
        self.shopkeeper    = LZString(info[@"shopkeeper"]);
        self.shopStatus    = ([info[@"status"] integerValue] == 1)?YES:NO;
       
        [self saveUserInfoToDisk];
    }
}
/// 相当于get方法
- (void)loadUserInfoFromDisk
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    self.ut  = [userDefaults objectForKey:kUT];
    
    self.merchantID    = [userDefaults objectForKey:kmerchantID];
    self.contactAddr   = [userDefaults objectForKey:kcontactAddr];
    self.contactMobile = [userDefaults objectForKey:kcontactMobile];
    self.contactName   = [userDefaults objectForKey:kcontactName];
    self.shopkeeper    = [userDefaults objectForKey:kshopkeeper];
    self.shopStatus    = [userDefaults boolForKey:kshopStatus];
    
}

- (BOOL) haveLogin{
    return self.ut?YES:NO;
}
#pragma mark - 要改变的信息
- (void) setUt:(NSString *)ut{
    _ut = LZString(ut);
    [self saveUserInfoToDisk];
}

- (void)saveUserInfoToDisk
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:self.ut forKey:kUT];
        
        [userDefaults setObject:self.merchantID forKey:kmerchantID];
        [userDefaults setObject:self.contactAddr forKey:kcontactAddr];
        [userDefaults setObject:self.contactMobile forKey:kcontactMobile];
        [userDefaults setObject:self.contactName forKey:kcontactName];
        [userDefaults setObject:self.shopkeeper forKey:kshopkeeper];
        [userDefaults setBool:self.shopStatus forKey:kshopStatus];
        
        [userDefaults synchronize];
    });
}

- (void)clearUserInfoFromDisk
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    [userDefaults removeObjectForKey:kUT];
    
    [userDefaults removeObjectForKey:kmerchantID];
    [userDefaults removeObjectForKey:kcontactAddr];
    [userDefaults removeObjectForKey:kcontactMobile];
    [userDefaults removeObjectForKey:kcontactName];
    [userDefaults removeObjectForKey:kshopkeeper];
    [userDefaults removeObjectForKey:kshopStatus];
  
    [userDefaults synchronize];
}

/// 清缓存
- (void)clearUserInfo
{
    self.haveLogin = NO;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        self.ut = nil;
        
        self.merchantID    = nil;
        self.contactAddr   = nil;
        self.contactMobile = nil;
        self.contactName   = nil;
        self.shopkeeper    = nil;
        self.shopStatus    = NO;
        
        [self clearUserInfoFromDisk];
    });
    
}


@end
